#ifndef AUDIOCAPTUREFF_H
#define AUDIOCAPTUREFF_H

#include "stdint.h"
#include "../third/portaudio/portaudio.h"
#include <vector>
#include <string>
//Windows
extern "C"
{
#include "libavcodec/avcodec.h"
#include "libavformat/avformat.h"
#include "libavutil/avutil.h"
#include "libswscale/swscale.h"
#include "libavutil/opt.h"
#include "libavutil/imgutils.h"
#include "libavdevice/avdevice.h"
#include "libavfilter/avfilter.h"
#include "libavfilter/buffersrc.h"
#include "libavfilter/buffersink.h"
};
#include <functional>
#include <dshow.h>
#include <windows.h>
#include "qedit.h"
#include <mutex>
#include <vector>
#include <thread>
#include "guiddef.h"
using namespace std;


class CaptureAudioFfmpeg {
public:
        class CaptureAudioObserver {
        public:
                virtual void OnAudioData(const void *frameaddress, uint32_t framelen) {};
        };
        typedef struct _T_MicInfo
        {
                wstring name;
                int index;
        }MICInfo;
        enum CAP_STATUS {
                RUNNING = 1,
                STOP = 2,
                PAUSE = 3,
                FAIL = 4,
        };

        vector<CaptureAudioFfmpeg::MICInfo> EnumSpeakers();
        CaptureAudioFfmpeg(uint16_t rate, uint8_t channel);
        int InitCapture(wstring url,uint16_t rate,uint8_t channel);

        /*
        ~CaptureAudio();
        int StartCapture();
        void StopCapture();
        int SetObserver(CaptureAudioObserver*);
        int OnCallBack(const void* input, void* output, unsigned long frameCount);
        void AddCnt(unsigned int x) {this->mSize += x;};
        */
private:
        std::thread mThread;
        uint16_t mSampleRate; //采样率
        uint16_t mChanel;    //通道号
        uint16_t mSamplefmt;
        unsigned long mSize;
        CAP_STATUS mStatus;
        CaptureAudioObserver *observer;
        int initAudioFilters();
        AVFormatContext *mInfmt_ctx = nullptr;
        AVFormatContext * mOutfmt_ctx = NULL;
        int64_t mLastReadPacktTime;
        AVFilterContext *mBuffersinkCtx = NULL;
        AVFilterContext *mBuffersrcCtx = NULL;
        AVFilterGraph *mFilterGraph = NULL;
        AVCodecContext*	mOutPutAudioEncContext = NULL;
        int64_t mAudioCount = 0;

        int flush_encoder(AVFormatContext *fmt_ctx, unsigned int stream_index);
        AVFrame *DecodeAudio(AVPacket* packet, AVFrame*pSrcAudioFrame);
};

#endif // AUDIOCAPTUREFF_H
